//
//  LBRConfigureModule.m
//  LBReact
//
//  Created by WuShiHai on 19/5/24.
//  Copyright (c) 2019 Lebbay. All rights reserved.
//

#import "LBRConfigureModule.h"
#import "LBRConstants.h"
#import "LBReactConfigurationHeader.h"
#import <LBFoundation/LBFoundation.h>
#import <AdSupport/AdSupport.h>
#import "LBRViewController.h"
#import "LBReactBridge.h"

@implementation LBRConfigureModule

RCT_EXPORT_MODULE(configure);

+ (BOOL)requiresMainQueueSetup {
    return YES;
}

- (NSDictionary<NSString *,id> *)constantsToExport{
    NSDictionary *launchOptions = [LBReactBridge manager].bridge.launchOptions ?: @{};
    return @{
             @"webAddress": LBBundleConfigureStringValue(LBReactServiceConfiguration, LBReactServiceWebAddressName, @"http://www.lebbay.react"),
             @"serverAddress": LBBundleConfigureStringValue(LBReactServiceConfiguration, LBReactServiceServerAddressName, @"http://www.lebbay.react"),
             @"environment": LBBundleConfigureStringValue(LBReactServiceConfiguration, LBReactServiceEnvironmentName, @"release"),
#if DEBUG
             @"debug": @(YES),
#else
             @"debug": @(NO),
#endif
             @"packageName": [LBDeviceUtil bundleIdentifier]?:@"",
             @"appVersion": AppReleaseVersionNumber()?:@"",
             @"deviceId": [LBDeviceUtil idfaString]?:@"",
             @"channel": LBBundleConfigureStringValue(LBReactServiceConfiguration, LBReactServiceChannelName, @"app_store"),
             @"model": [LBDeviceUtil getDeviceName]?:@"",
             @"systemVersion": [UIDevice currentDevice].systemVersion?:@"",
             @"requestSalt": @"sjdo12379341jik890879&*#@$^&*Q*&",
             @"launchOptions": launchOptions,
             @"launchOptionsRemoteNotification": launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]?:@{},
#if DEBUG || TESTRELEASE
             @"isLog": @"1",
#else
             @"isLog": @"0",
#endif
             };
}

RCT_EXPORT_METHOD(fetchClientIP:(RCTPromiseResolveBlock)resolve fail:(RCTPromiseRejectBlock)reject){
    beginExecInMainBlock
    NSString *ip = [LBDeviceUtil getIPAddress];
    resolve?resolve(@{@"ip": ip?:@""}):nil;
    endExecInMainBlock
}

RCT_EXPORT_METHOD(changeStatusBarStyle:(NSString *)moduleId style:(NSString *)style){
    beginExecInMainBlock
    NSDictionary *parameters = @{@"style": style?:@"", @"moduleId": moduleId?:@""};
    [[NSNotificationCenter defaultCenter] postNotificationName:LBRChangeStatusBarStyleNotification
                                                        object:parameters];
    endExecInMainBlock
}

//js初始化完后 回调原生加载主页面
RCT_EXPORT_METHOD(loadAppPage){
    beginExecInMainBlock
    LBBlockSafeRun([LBReactBridge manager].callback);
    endExecInMainBlock
}

//页面渲染完成
RCT_EXPORT_METHOD(loadRNPageCompleted){
    beginExecInMainBlock
    LBBlockSafeRun([LBReactBridge manager].renderCallback);
    endExecInMainBlock
}

@end
